import { Context, State } from './state';
enum TrasitionType{
    Next,
    Complete
}
class GlassData{
    pwoer:string = '';
}
class GlassState extends State<TrasitionType>{
    data:GlassData;
    constructor(name:string, data:GlassData){
        super(name);
        this.data = data;
    }
    onEntered = (type: TrasitionType) => {
        this.data.pwoer = this.name;
      //  console.log(`${this.name} onEntered`);
    };
    onExited = (type: TrasitionType) => {
       // console.log(`${this.name} onExited`);
    };
}
class YellowState extends GlassState{ 
    constructor(name:string, data:GlassData){
        super(name, data); 
    }
    onEntered = (type: TrasitionType) => {
        this.data.pwoer = 'yellow';
       // console.log(`${this.name} onEntered`);
    }; 
}
 
function run() {
    const context = new Context<TrasitionType>();
    const data = new GlassData();
    const redState = new GlassState('red', data);
    const redYellowState = new YellowState('red-yellow', data); 
    const greenYellowState = new YellowState('green-yellow', data);
    const greenState = new GlassState('green', data);
    const noState = new GlassState('no_power', data); 
    noState.addTransition(TrasitionType.Next, redState);
    noState.addTransition(TrasitionType.Complete, noState);
    redState.addTransition(TrasitionType.Next, redYellowState);
    redState.addTransition(TrasitionType.Complete, noState);
    redYellowState.addTransition(TrasitionType.Next, greenState); 
    redYellowState.addTransition(TrasitionType.Complete, noState);
    greenYellowState.addTransition(TrasitionType.Next, redState);
    greenYellowState.addTransition(TrasitionType.Complete, noState);
    
    greenState.addTransition(TrasitionType.Next, greenYellowState);
    greenState.addTransition(TrasitionType.Complete, noState);
    context.initState(noState);
    let counter = 20;
    while(counter-- > 0){  
        //console.log(`第${counter}次循环`); 
        let nextState = context.doTransition(TrasitionType.Next); 
        if(!nextState){
            console.log(`状态转换失败，未找到${context.currentState?.name}-${TrasitionType.Next}的转换状态`);
            break;
        }
        console.log(`最后状态${context.currentState?.name} -  灯的颜色${data.pwoer}`);
    }
    context.doTransition(TrasitionType.Complete);
    console.log(`最后状态${context.currentState?.name}`);
}
 

console.log('启动测试');
run(); 
console.log('结束测试');